
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

String _userErrorText;
String _passwordErrorText;
_UserFieldState _userFieldState;
_PasswordFieldState _passwordFieldState;

class Login extends StatefulWidget{
  @override
  LoginState createState()=>new LoginState();
}

class LoginState extends State<Login>{
  final TextEditingController userTextEdit = new TextEditingController();
  final TextEditingController passwordTextEdit = new TextEditingController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print('哈哈哈我到登录界面了');
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
        title: Text("登录"),
      ),
      body: Container(
        child: Stack(
          children: <Widget>[
            Positioned(
              bottom: 36,
              left: 12,
              child: Container(
                alignment: Alignment.centerLeft,
                margin: EdgeInsets.fromLTRB(0, 6, 24, 0),
                child: RaisedButton(
                  child: Text("注册账户"),
                  textColor: Colors.blue,
                  onPressed: (){
                    Navigator.pushNamed(context, "Register");
                  },
                  color: Colors.transparent,
                  disabledColor: Colors.blue,
                  elevation: 0,
                  highlightElevation: 0,
                ),
              ),
            ),
            Positioned(
              bottom: 36,
              right: 24,
              child: Container(
                child: RaisedButton(
                  child: Text("下一步"),
                  textColor: Colors.white,
                  onPressed: (){
                  },
                  color: Colors.blue,
                  disabledColor: Colors.blue,
                ),
              ),
            ),
            Positioned(
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.all(12.0),
                    child: Text(
                      '登录',
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 28.0,
                        height: 1.2,
                      ),
                    ),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.max,
                    children: <Widget>[
                      Text(
                        '请登录您的账号。',
                        style: TextStyle(
                            color: Colors.black, fontSize: 15.0, height: 1.0),
                      ),
                      Text(
                        '了解详情',
                        style: TextStyle(
                            color: Colors.blue, fontSize: 15.0, height: 1.0),
                      )
                    ],
                  ),
                  Container(
                      margin: EdgeInsets.fromLTRB(24, 12, 24, 12),
                      child:_UserTextFiled("请输入您的用户名", "用户名",userTextEdit)
                  ),
                  Container(
                    margin: EdgeInsets.fromLTRB(24, 12, 24, 12),
                    child: _PasswordField("请输入密码", "密码",passwordTextEdit),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}


class _UserTextFiled extends StatefulWidget{
  final String _hint;
  final String _labelText;
  final TextEditingController _textEditingController;
  _UserTextFiled(this._hint,this._labelText,this._textEditingController);
  @override
  State<StatefulWidget> createState() {
    return _userFieldState = _UserFieldState(_hint,_labelText,_textEditingController);
  }

}

class _UserFieldState extends State{
  final TextEditingController _textEditingController;
  final String _hintText;
  final String _labelText;
  _UserFieldState(this._hintText,this._labelText,this._textEditingController);
  @override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return TextField(
      keyboardType: TextInputType.text,
      decoration: InputDecoration(
        contentPadding: EdgeInsets.all(16),
        hintText: _hintText,
        errorText: _userErrorText,
        prefixIcon: Icon(Icons.person),
        enabledBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 1,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        focusedBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 3,
            color: Colors.blue,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        errorBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 3,
            color: Colors.red,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        focusedErrorBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 3,
            color: Colors.red,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        labelText: _labelText,
      ),
      controller: _textEditingController,
    );
  }

}

class _PasswordField extends StatefulWidget{
  final String _hintText;
  final String _labelText;
  final TextEditingController _textEditingController;
  _PasswordField(this._hintText,this._labelText,this._textEditingController);
  @override
  State<StatefulWidget> createState() {
    return _passwordFieldState = _PasswordFieldState(_hintText,_labelText,_textEditingController);
  }

}

class _PasswordFieldState extends State{
  bool canSee;
  final TextEditingController _textEditingController;
  final String _hintText;
  final String _labelText;
  _PasswordFieldState(this._hintText,this._labelText,this._textEditingController);

  @override
  void initState() {
    super.initState();
    canSee = false;
  }
  @override
  Widget build(BuildContext context) {
    return TextField(
      decoration: InputDecoration(
        contentPadding: EdgeInsets.all(16),
        hintText: _hintText,
        errorText: _passwordErrorText,
        prefixIcon: Icon(Icons.lock),
        suffixIcon: IconButton(
          icon: Icon(canSee ? Icons.visibility : Icons.visibility_off),
          onPressed: (){
            setState(() {
              canSee = !canSee;
            });
          },
        ),
        enabledBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 1,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        focusedBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 3,
            color: Colors.blue,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        errorBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 3,
            color: Colors.red,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        focusedErrorBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 3,
            color: Colors.red,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        labelText: _labelText,
      ),
      obscureText: !canSee,
      controller: _textEditingController,
    );
  }
}